﻿// <copyright file="DomainTurnAddEditOwner.cs" company="Gravity Age Studios">
// Code released under the MIT license (see License.txt).
// </copyright>
// <author>Vicente Cartas Espinel</author>
// <email>vicente.cartas@gmail.com</email>
namespace LightSwitchApplication
{
    using System.Collections.Generic;
    using LightSwitchApplication.UserCode;
    using Microsoft.LightSwitch;
    using Microsoft.LightSwitch.Presentation.Extensions;

    public partial class DomainTurnAddEditOwner
    {
        partial void DomainTurnAddEditOwner_InitializeDataWorkspace(List<IDataService> saveChangesTo)
        {
            if (this.DomainTurnId == null)
            {
                this.DomainTurn = new DomainTurn();
                this.DisplayName = "Add new Domain Turn";

                this.DomainTurn.Visibility = (int)VisibilityEnum.Owner;
                this.DomainTurn.State = (int)DomainTurnStateEnum.Created;
            }
            else
            {
                this.DomainTurn = this.DomainTurnQuery;
                this.DisplayName = string.Format("Edit Domain Turn {0} for {1}", this.DomainTurn.Turn, this.DomainTurn.Domain);

                // A turn that has been Closed or Resolved by a GM can not be edited further
                if (this.DomainTurn.State == (int)DomainTurnStateEnum.Closed ||
                    this.DomainTurn.State == (int)DomainTurnStateEnum.Resolved)
                {
                    this.FindControl("Domain").IsEnabled = false;
                    this.FindControl("Turn").IsEnabled = false;
                    this.FindControl("TurnOwnerInformation").IsEnabled = false;

                    this.FindControl("FirstRoundActions").IsEnabled = false;
                    this.FindControl("FirstRoundActionsAddAndEditNew").IsEnabled = false;
                    this.FindControl("FirstRoundActionsEditSelected").IsEnabled = false;
                    this.FindControl("FirstRoundActions_DeleteSelected").IsEnabled = false;
                    this.FindControl("FirstRoundOwnerInformation").IsEnabled = false;

                    this.FindControl("SecondRoundActions").IsEnabled = false;
                    this.FindControl("SecondRoundActionsAddAndEditNew").IsEnabled = false;
                    this.FindControl("SecondRoundActionsEditSelected").IsEnabled = false;
                    this.FindControl("SecondRoundActions_DeleteSelected").IsEnabled = false;
                    this.FindControl("SecondRoundOwnerInformation").IsEnabled = false;

                    this.FindControl("ThirdRoundActions").IsEnabled = false;
                    this.FindControl("ThirdRoundActionsAddAndEditNew").IsEnabled = false;
                    this.FindControl("ThirdRoundActionsEditSelected").IsEnabled = false;
                    this.FindControl("ThirdRoundActions_DeleteSelected").IsEnabled = false;
                    this.FindControl("ThirdRoundOwnerInformation").IsEnabled = false;
                }
            }
        }

        #region Turn Submission

        partial void SubmitTurn_CanExecute(ref bool result)
        {
            result = this.DomainTurn.State == (int)DomainTurnStateEnum.Created;
        }

        partial void SubmitTurn_Execute()
        {
            // Try to save to see if there are problems with the DomainTurn
            this.DataWorkspace.ApplicationData.SaveChanges();

            this.DomainTurn.State = (int)DomainTurnStateEnum.Submitted;
            this.DataWorkspace.ApplicationData.SaveChanges();

            this.Close(false);
        }

        #endregion

        #region Add/Edit DomainAction

        partial void FirstRoundActionsAddAndEditNew_Execute()
        {
            this.DomainAction =  this.FirstRoundActions.AddNew();
            this.DomainAction.Visibility = (int)VisibilityEnum.Public;
            this.DomainAction.DomainTurn1 = this.DomainTurn;

            this.FindControl("ModalWindowDomainAction").DisplayName = "Add new Domain Action";
            this.OpenModalWindow("ModalWindowDomainAction");
        }

        partial void FirstRoundActionsEditSelected_CanExecute(ref bool result)
        {
            result = this.FirstRoundActions.SelectedItem != null;
        }

        partial void FirstRoundActionsEditSelected_Execute()
        {
            this.DomainAction = this.FirstRoundActions.SelectedItem;

            this.FindControl("ModalWindowDomainAction").DisplayName = "Edit Domain Action";
            this.OpenModalWindow("ModalWindowDomainAction");
        }

        partial void SecondRoundActionsAddAndEditNew_Execute()
        {
            this.DomainAction = this.SecondRoundActions.AddNew();
            this.DomainAction.Visibility = (int)VisibilityEnum.Public;
            this.DomainAction.DomainTurn2 = this.DomainTurn;

            this.FindControl("ModalWindowDomainAction").DisplayName = "Add new Domain Action";
            this.OpenModalWindow("ModalWindowDomainAction");
        }

        partial void SecondRoundActionsEditSelected_CanExecute(ref bool result)
        {
            result = this.SecondRoundActions.SelectedItem != null;
        }

        partial void SecondRoundActionsEditSelected_Execute()
        {
            this.DomainAction = this.SecondRoundActions.SelectedItem;

            this.FindControl("ModalWindowDomainAction").DisplayName = "Edit Domain Action";
            this.OpenModalWindow("ModalWindowDomainAction");
        }

        partial void ThirdRoundActionsAddAndEditNew_Execute()
        {
            this.DomainAction = this.ThirdRoundActions.AddNew();
            this.DomainAction.Visibility = (int)VisibilityEnum.Public;
            this.DomainAction.DomainTurn3 = this.DomainTurn;

            this.FindControl("ModalWindowDomainAction").DisplayName = "Add new Domain Action";
            this.OpenModalWindow("ModalWindowDomainAction");
        }

        partial void ThirdRoundActionsEditSelected_CanExecute(ref bool result)
        {
            result = this.ThirdRoundActions.SelectedItem != null;
        }

        partial void ThirdRoundActionsEditSelected_Execute()
        {
            this.DomainAction = this.ThirdRoundActions.SelectedItem;

            this.FindControl("ModalWindowDomainAction").DisplayName = "Edit Domain Action";
            this.OpenModalWindow("ModalWindowDomainAction");
        }

        partial void SubmitModalWindowDomainAction_Execute()
        {
            this.CloseModalWindow("ModalWindowDomainAction");
            this.DataWorkspace.ApplicationData.SaveChanges();
        }

        partial void CancelModalWindowDomainAction_Execute()
        {
            this.CloseModalWindow("ModalWindowDomainAction");

            if (this.FirstRoundActions.SelectedItem != null)
            {
                this.FirstRoundActions.SelectedItem.Details.DiscardChanges();
            }

            if (this.SecondRoundActions.SelectedItem != null)
            {
                this.SecondRoundActions.SelectedItem.Details.DiscardChanges();
            }

            if (this.ThirdRoundActions.SelectedItem != null)
            {
                this.ThirdRoundActions.SelectedItem.Details.DiscardChanges();
            }
        }

        #endregion
    }
}